package basic.practice;


//加载驱动、建立连接、释放资源
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 
 * @author 42276
 * @author 任雪燕
 * @author 24372
 * @author 15869
 * @author admin
 * @author 张冰锋
 * @author 清风依旧
 * Database test class
 */
public class TestJDBC {
	private static String url,user,pwd;
	static {	
		url="jdbc:mysql://localhost:3306/student_info";
		user="cms";
		pwd="root";			
		try {
//加载驱动				
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
//建立连接
		public static Connection getConn() {			
			Connection conn=null;
			try {
				conn = DriverManager.getConnection(url,user,pwd);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}					
			return conn;		
		}
//释放资源
		public static void free(ResultSet rs,PreparedStatement ps,Connection conn) {
			if(rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ps!=null) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}		
		}
}